package com.sunseagear.wind.modules.test.twotable.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sunseagear.common.base.http.R;
import com.sunseagear.common.base.http.RPage;
import com.sunseagear.common.base.mvc.controller.BaseBeanController;
import com.sunseagear.common.utils.StringUtils;
import com.sunseagear.wind.common.aspectj.annotation.Log;
import com.sunseagear.wind.common.aspectj.enums.LogType;
import com.sunseagear.wind.modules.test.twotable.entity.CarModel;
import com.sunseagear.wind.modules.test.twotable.service.ICarModelService;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.validation.annotation.Validated;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 *
 * @version V1.0
 * @package com.sunseagear.wind.modules.sys.controller
 * @title: 消息模版控制器
 * @description: 消息模版控制器 * @date: 2018-09-03 15:10:10
 * @copyright: 2018 www.sunseagear.com Inc. All rights reserved.
 */

@RestController
@RequestMapping("/test/twotable/carmodel")
@Log(title = "车辆型号管理")
@RequiredArgsConstructor
public class CarModelController extends BaseBeanController {

    private final ICarModelService carModelService;


    /**
     * 根据页码和每页记录数，以及查询条件动态加载数据
     */
    @PostMapping(value = "list")
    @Log(logType = LogType.SELECT)
    @SaCheckPermission("test:twotable:car:list")
    public RPage<CarModel> list(HttpServletRequest request) {
        //加入条件
        QueryWrapper<CarModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByAsc("sort");
        String keyword = request.getParameter("keyword");
        String carId = request.getParameter("carId");
        if (!StringUtils.isEmpty(carId) && !StringUtils.isEmpty(keyword)) {
            queryWrapper.eq("car_id", carId).and(i -> i.like("label", keyword).or().like("value", keyword));
        } else if (!StringUtils.isEmpty(carId)) {
            queryWrapper.eq("car_id", carId);
        }

        // 预处理
        Page<CarModel> pageBean = carModelService.page(getPage(), queryWrapper);
        return RPage.success(pageBean);
    }

    @GetMapping("detail/{id}")
    @Log(logType = LogType.SELECT)
    @SaCheckPermission("test:twotable:car:detail")
    public R<CarModel> detail(@PathVariable("id") Long id) {
        CarModel carModel = carModelService.getById(id);
        return R.success(carModel);
    }

    @PostMapping("add")
    @Log(logType = LogType.UPDATE)
    @SaCheckPermission("add")
    public R<Void> add(@Validated CarModel entity) {
        carModelService.save(entity);
        return R.success("添加成功");
    }

    @PostMapping("update")
    @Log(logType = LogType.UPDATE)
    @SaCheckPermission("test:twotable:car:update")
    public R<Void> update(@Validated CarModel entity) {
        carModelService.saveOrUpdate(entity);
        return R.success("更新成功");
    }

    @PostMapping("delete")
    @Log(logType = LogType.DELETE)
    @SaCheckPermission("test:twotable:car:delete")
    public R<Void> delete(@RequestParam("ids") List<Long> idList) {
        carModelService.removeBatchByIds(idList);
        return R.success("删除成功");
    }

}
